home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / Label.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  8.2 KB  |  277 lines  |  [TEXT/CWIE]

  1. // TextField.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1997 Netscape Communications Corp. All rights reserved.
  4. package netscape.application;
  5.  
  6. import netscape.util.*;
  7.  
  8. /** A view subclass to implement a label. A label is a small string, used to
  9.   * indicate the purpose of a control. A label has a command binded with a key.
  10.   * When the key is pressed, the label send the command to a view. The view is
  11.   * usualy the view labeled by the label. A label is always transparent.
  12.   *
  13.   */
  14. public class Label extends View implements Target {
  15.     TextField label;
  16.     Target    target;
  17.     String    command;
  18.     Rect      underlineRect = null;
  19.     int       key;
  20.  
  21.     static final String LABEL_KEY =  "label";
  22.     static final String TARGET_KEY = "target";
  23.     static final String COMMAND_KEY = "command";
  24.     static final String KEY_KEY = "labelKey";
  25.  
  26.     final int    UNDERLINE_SIZE = 0;
  27.     final String SEND_COMMAND   = "sendCommand";
  28.  
  29.     /** Empty constructor, used for unarchiving. **/
  30.     public Label() {
  31.         this("",null);
  32.     }
  33.  
  34.     /** Create a new label displaying <b>title</b>. The new label
  35.       * will have the minimum size to fit <b>title</b> with <b>font</b>.
  36.       * <b>font</b> can be null. In this case <b>Font.defaultFont()</b>
  37.       * will be used.
  38.       */
  39.     public Label(String title, Font aFont) {
  40.         super();
  41.  
  42.         label = new TextField(0, 0, 0,0);
  43.         label.setBorder(null);
  44.         label.setStringValue(title);
  45.         label.setFont(aFont);
  46.         label.setTransparent(true);
  47.         label.setEditable(false);
  48.         label.setSelectable(false);
  49.         label.setJustification(Graphics.RIGHT_JUSTIFIED);
  50.         addSubview(label);
  51.         sizeToMinSize();
  52.     }
  53.  
  54.     /** Set the label justification. <b>aJustification</b> can
  55.       * be Graphics.LEFT_JUSTIFIED, Graphics.CENTERED or
  56.       * Graphics.RIGHT_JUSTIFIED. The default value is
  57.       * Graphics.LEFT_JUSTIFIED
  58.       */
  59.     public void setJustification(int aJustification) {
  60.         label.setJustification(aJustification);
  61.     }
  62.  
  63.     /** Return the justification for this label **/
  64.     public int justification() {
  65.         return label.justification();
  66.     }
  67.  
  68.     /** Set the label title **/
  69.     public void setTitle(String aTitle) {
  70.         label.setStringValue(aTitle);
  71.         invalidateUnderlineRect();
  72.     }
  73.  
  74.     /** Return the label title **/
  75.     public String title() {
  76.         return label.stringValue();
  77.     }
  78.  
  79.     /** Set the label font **/
  80.     public void setFont(Font aFont) {
  81.         label.setFont(aFont);
  82.         invalidateUnderlineRect();
  83.     }
  84.  
  85.     /** Return the label font **/
  86.     public Font font() {
  87.         return label.font();
  88.     }
  89.  
  90.     /** Returns the View's minimum size.
  91.       * the minimum size is always the minimum size
  92.       * to fit the label
  93.       * @see #setMinSize
  94.       */
  95.     public Size minSize() {
  96.         Font font = label.font();
  97.         FontMetrics metrics;
  98.         int width,height;
  99.  
  100.         metrics = font.fontMetrics();
  101.         width  = metrics.stringWidth(label.stringValue());
  102.         height = metrics.stringHeight();
  103.         return new Size(width,height);
  104.     }
  105.  
  106.     /**
  107.       * Overriden to invalidate the underline rect
  108.       * and resize the underline field
  109.       */
  110.     public void didSizeBy(int deltaWidth, int deltaHeight) {
  111.         super.didSizeBy(deltaWidth,deltaHeight);
  112.         invalidateUnderlineRect();
  113.         label.setBounds(0,0,width(),height()-UNDERLINE_SIZE);
  114.     }
  115.  
  116.     /** Set the label text color **/
  117.     public void setColor(Color aColor) {
  118.         label.setTextColor(aColor);
  119.     }
  120.  
  121.     /** Return the label text color **/
  122.     public Color color() {
  123.         return label.textColor();
  124.     }
  125.  
  126.     /** Set the label target. The target is the object that will receive
  127.       * a command when the key associated with this label is pressed.
  128.       */
  129.     public void setTarget(Target aTarget) {
  130.         target = aTarget;
  131.     }
  132.  
  133.     /** Return the label target. **/
  134.     public Target target() {
  135.         return target;
  136.     }
  137.  
  138.     /** Set the label command. The label command is sent to the target
  139.       * when the key associated with this label is pressed.
  140.       */
  141.     public void setCommand(String aCommand) {
  142.         command = aCommand;
  143.     }
  144.  
  145.     /** Return the label command **/
  146.     public String command() {
  147.         return command;
  148.     }
  149.  
  150.     /** Set the key that should be pressed for
  151.       * this label to send its command. The letter matching <b>aKey</b>
  152.       * will be underlined. Pressing the key when no view has the focus
  153.       * will send the command. When a view has the focus, pressing ALT + key
  154.       * will send the command.
  155.      */
  156.     public void setCommandKey(int aKey) {
  157.         key = aKey;
  158.         invalidateUnderlineRect();
  159.         removeAllCommandsForKeys();
  160.         setCommandForKey(SEND_COMMAND,null,aKey,KeyEvent.NO_MODIFIERS_MASK,View.WHEN_IN_MAIN_WINDOW);
  161.         setCommandForKey(SEND_COMMAND,null,aKey,KeyEvent.CONTROL_MASK,View.WHEN_IN_MAIN_WINDOW);
  162.         setDirty(true);
  163.     }
  164.  
  165.     /** Returns the key that will fire the command **/
  166.     public int commandKey() {
  167.         return key;
  168.     }
  169.  
  170.     /** Overridden to return <b>true</b>
  171.       */
  172.     public boolean isTransparent() {
  173.         return true;
  174.     }
  175.  
  176.     /** Target implementation.
  177.       * @private
  178.       */
  179.     public void performCommand(String command, Object data) {
  180.         if(SEND_COMMAND.equals(command)) {
  181.             sendCommand();
  182.         }
  183.     }
  184.  
  185.     void invalidateUnderlineRect() {
  186.         underlineRect = null;
  187.     }
  188.  
  189.     /** This method is called by drawView() to discover the rect of
  190.       * the black line used to underline the character matching the
  191.       * key. Use this method if you want to draw the underline in
  192.       * a different way.
  193.       */
  194.     public Rect underlineRect() {
  195.         int index;
  196.  
  197.         if(underlineRect == null) {
  198.             if(key != 0) {
  199.                 String s = label.stringValue();
  200.                 index = s.indexOf(key);
  201.                 if(index == -1) {
  202.                     index = s.toUpperCase().indexOf(key);
  203.                     if(index==-1)
  204.                         index = s.toLowerCase().indexOf(key);
  205.                 }
  206.  
  207.                 if(index != -1) {
  208.                     Rect r = label.rectForRange(index,index + 1);
  209.  
  210.                     underlineRect = new Rect();
  211.                     label.convertRectToView(this,r,underlineRect);
  212.                     underlineRect.y = underlineRect.y + underlineRect.height + UNDERLINE_SIZE - 1;
  213.                     underlineRect.height = 1;
  214.                 }
  215.             }
  216.  
  217.             if(underlineRect == null)
  218.                 underlineRect = new Rect(0,0,0,0);
  219.         }
  220.         return underlineRect;
  221.     }
  222.  
  223.     /** Overriden to underline the letter that matches the key **/
  224.     public void drawView(Graphics g) {
  225.         Rect underlineRect = underlineRect();
  226.  
  227.         if(underlineRect != null && underlineRect.intersects(g.clipRect())) {
  228.             g.setColor(label.textColor());
  229.             g.fillRect(underlineRect());
  230.         }
  231.     }
  232.  
  233.     void sendCommand() {
  234.         if(target != null && command != null)
  235.             target.performCommand(command,this);
  236.     }
  237.  
  238. /* archiving */
  239.  
  240.     /** Describes the TextField class' information.
  241.       * @see Codable#describeClassInfo
  242.       */
  243.     public void describeClassInfo(ClassInfo info) {
  244.         super.describeClassInfo(info);
  245.  
  246.         info.addClass("netscape.application.Label",1);
  247.         info.addField(LABEL_KEY,OBJECT_TYPE);
  248.         info.addField(TARGET_KEY,OBJECT_TYPE);
  249.         info.addField(COMMAND_KEY,STRING_TYPE);
  250.         info.addField(KEY_KEY,INT_TYPE);
  251.     }
  252.  
  253.     /** Encodes the TextField instance.
  254.       * @see Codable#decode
  255.       */
  256.     public void encode(Encoder encoder) throws CodingException {
  257.         super.encode(encoder);
  258.  
  259.         encoder.encodeObject(LABEL_KEY,label);
  260.         encoder.encodeObject(TARGET_KEY,(Codable)target);
  261.         encoder.encodeString(COMMAND_KEY,command);
  262.         encoder.encodeInt(KEY_KEY,key);
  263.     }
  264.  
  265.     /** Decodes the TextField instance.
  266.       * @see Codable#decode
  267.       */
  268.     public void decode(Decoder decoder) throws CodingException {
  269.         super.decode(decoder);
  270.  
  271.         label   = (TextField) decoder.decodeObject(LABEL_KEY);
  272.         target  = (Target)    decoder.decodeObject(TARGET_KEY);
  273.         command = decoder.decodeString(COMMAND_KEY);
  274.         key     = decoder.decodeInt(KEY_KEY);
  275.     }
  276. }
  277.